package com.xkstream.client.http;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.xkstream.client.utils.LogUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;


public class FileUploadUtil {

    /**
     * 上传图片
     *
     * @param urlStr
     * @param textMap
     * @param fileMap
     * @return
     */
    private static String TAG = "FileUploadUtil";
    public static final int SUCCESS = 0x101;
    public static final int FAILED = 0x102;
    public static final String RESULT = "RESULT";

    public static String uploadImageFile(String urlStr, Map<String, String> textMap, Map<String, String> fileMap, Handler handler, String name) {
        String res = "";
        LogUtil.e(TAG, "url : " + urlStr);
        HttpURLConnection conn = null;
        String BOUNDARY = "---------------------------123821742118716"; //boundary就是request头和上传文件内容的分隔符    
        try {
            URL url = new URL(urlStr);
            conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(30000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
            OutputStream out = new DataOutputStream(conn.getOutputStream());
            // text    
            if (textMap != null) {
                StringBuffer strBuf = new StringBuffer();
                Iterator<Map.Entry<String, String>> iter = textMap.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry<String, String> entry = iter.next();
                    String inputName = (String) entry.getKey();
                    String inputValue = (String) entry.getValue();
                    if (inputValue == null) {
                        continue;
                    }
                    strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
                    strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
                    strBuf.append(inputValue);
                }
                out.write(strBuf.toString().getBytes());
            }

            // file    
            if (fileMap != null) {
                Iterator<Map.Entry<String, String>> iter = fileMap.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry<String, String> entry = iter.next();
                    String inputName = (String) entry.getKey();
                    String inputValue = (String) entry.getValue();
                    if (inputValue == null) {
                        continue;
                    }
                    File file = new File(inputValue);
                    String filename = file.getName();
                    /*MagicMatch match = Magic.getMagicMatch(file, false, true);
                    String contentType = match.getMimeType();*/

                    StringBuffer strBuf = new StringBuffer();
                    strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
                    strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" + filename + "\"\r\n");
                    strBuf.append("Content-Type:" + "image/jpg" + "\r\n\r\n");

                    out.write(strBuf.toString().getBytes());

                    DataInputStream in = new DataInputStream(new FileInputStream(file));
                    int bytes = 0;
                    byte[] bufferOut = new byte[1024];
                    while ((bytes = in.read(bufferOut)) != -1) {
                        out.write(bufferOut, 0, bytes);
                    }
                    in.close();
                }
            }

            byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
            out.write(endData);
            out.flush();
            out.close();

            // 读取返回数据    
            StringBuffer strBuf = new StringBuffer();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                strBuf.append(line).append("\n");
            }
            res = strBuf.toString();
            res = getUrlFromJson(res);
            Message msg = handler.obtainMessage();
            msg.what = SUCCESS;
            msg.obj = name;
            Bundle bundle = new Bundle();
            bundle.putString(RESULT, res);
            msg.setData(bundle);
            handler.sendMessage(msg);
            reader.close();
            reader = null;

        } catch (Exception e) {
            if (handler != null) {
                Message msg = handler.obtainMessage();
                msg.what = FAILED;
                msg.obj = name;
                Bundle bundle = new Bundle();
                res = getUrlFromJson(res);
                bundle.putString(RESULT, res);
                msg.setData(bundle);
                handler.sendMessage(msg);
                e.printStackTrace();
            }
        } finally {
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
        }
        LogUtil.e("FileUploadUtil", "result : " + res);
        return res;
    }

    private static String getUrlFromJson(String str){
        try {
            JSONObject jsonObject = new JSONObject(str);
            if (jsonObject.has("code")){
                int code = jsonObject.getInt("code");
                if (code == 0){//失败
                    return jsonObject.getString("msg");
                }else { //成功
                    JSONObject jsonData= jsonObject.getJSONObject("data");
                    return  jsonData.getString("picPath");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}  